package com.unknown.wificontrol.ui.home;

import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.unknown.wificontrol.R;
import com.unknown.wificontrol.http.Http;
import com.unknown.wificontrol.http.HttpListener;
import com.unknown.wificontrol.listener.HXItemSelectListener;
import com.unknown.wificontrol.listener.OnDialogListener;
import com.unknown.wificontrol.ui.BaseActivity;
import com.unknown.wificontrol.util.Preference;
import com.unknown.wificontrol.widget.BottomPopItemView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public class IncentiveSettingActivity extends BaseActivity {
    @Override
    public int getLayout() {
        return R.layout.layout_incentive_setting;
    }

    @Override
    public void initViews() {
        etFrequency.setText(Preference.get(this, "frequency", "5000"));
        etFrequency.setSelection(etFrequency.getText().toString().length());
        etWave.setText(Preference.get(this, "Wave", "方波"));
        etNumberOfCycles.setText(Preference.get(this, "NumberOfCycles", "1"));
    }

    @BindView(R.id.etFrequency)
    EditText etFrequency;

    @BindView(R.id.etWave)
    EditText etWave;

    @BindView(R.id.etNumberOfCycles)
    EditText etNumberOfCycles;

    public void onItem(View v) {
        switch (v.getId()) {
            case R.id.ivBX: {
                BottomPopItemView item = new BottomPopItemView(mActivity);
                final String[] items = new String[]{"方波", "脉冲波"};
                item.show(etWave.getText().toString(), items, new HXItemSelectListener() {
                    @Override
                    public void onSelected(int index) {
                        etWave.setText(items[index]);
                        etWave.setSelection(etWave.getText().toString().length());
                    }
                });
            }
            break;
            case R.id.ivZQ: {
                BottomPopItemView item = new BottomPopItemView(mActivity);
                final String[] items = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
                item.show(etNumberOfCycles.getText().toString(), items, new HXItemSelectListener() {
                    @Override
                    public void onSelected(int index) {
                        etNumberOfCycles.setText(items[index]);
                        etNumberOfCycles.setSelection(etNumberOfCycles.getText().toString().length());
                    }
                });
            }
            break;
        }
    }

    public void onCommit(View v) {
        String frequency = etFrequency.getText().toString();
        String Wave = etWave.getText().toString();
        String NumberOfCycles = etNumberOfCycles.getText().toString();
        if (isEmpty(frequency)) {
            showToast("请输入频率");
            return;
        }
        if (isEmpty(Wave)) {
            showToast("请输入波形");
            return;
        }
        if (isEmpty(NumberOfCycles)) {
            showToast("请输入周期数");
            return;
        }
        JSONObject object = new JSONObject();
        try {
            object.put("frequency", frequency);
            object.put("Wave", Wave);
            object.put("NumberOfCycles", NumberOfCycles);
        } catch (Exception exception) {

        }
        showProgress("设置中...");
        Http.post(getHost() + "/getValues/Incentive", object.toString(), new HttpListener() {
            @Override
            public void onReturn(JSONObject jsonObject) {
                hideProgress();
                if (jsonObject != null && jsonObject.length() > 0) {
                    int code = jsonObject.optInt("code");
                    String message = jsonObject.optString("message");
                    if (code == 200) {
                        Preference.save(mActivity, "frequency", frequency);
                        Preference.save(mActivity, "Wave", Wave);
                        Preference.save(mActivity, "NumberOfCycles", NumberOfCycles);
                        showSuccess("激励设置成功", new OnDialogListener(){
                            @Override
                            public void onConfirm() {
                                super.onConfirm();
                                finish();
                            }
                        });
                    } else {
                        showToast(message);
                    }
                } else {
                    showToast("请求失败");
                }

            }
        });

    }
}
